package com.example.math;

import java.io.InputStream;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SecondScreenList extends Activity implements OnClickListener {
	TextView headerText;
	Button homeBtn;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.listview);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.titlebar);
		homeBtn = (Button) findViewById(R.id.home_btn);
		headerText = (TextView) findViewById(R.id.header_txt);
		headerText.setText("Screen Two");
		homeBtn.setOnClickListener(this);
		// Create Parser for raw/imgtextfile.xml
		SecondScreenListParser listParser = new SecondScreenListParser();
		InputStream inputStream = getResources().openRawResource(
				R.raw.imgtextfile);
		
		// Parse the inputstream
		listParser.parse(inputStream);

		// Get items
		List<SecondScreenListClass> parsedList = listParser.getList();
		
		
		// Create a customized ArrayAdapter
		SecondScreenListArrayAdapter adapter = new SecondScreenListArrayAdapter(
				getApplicationContext(), R.layout.listitem, parsedList);
		
		// Get reference to ListView holder
		final ListView lv = (ListView) this.findViewById(R.id.itemLV);
		
		// Set the ListView adapter
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long id) {
				Intent i = new Intent(getApplicationContext(),ThirdScreen.class);
				Bundle b = new Bundle();
				SecondScreenListClass selectedClass ;
				selectedClass= (SecondScreenListClass) lv.getItemAtPosition(position);
				String selectedText = selectedClass.name;
				b.putString("SecondScreenItem",selectedText );
				i.putExtras(b);
				startActivity(i);				
			}
		});
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.home_btn:

			Intent intentHome = new Intent(this, FirstScreen.class);
			startActivity(intentHome);
			break;

		default:
			
			break;
		}	
		}
	
}